package com.ruoyi.goods.controller;

import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.goods.config.DelayedConfig2;
import com.ruoyi.goods.mapper.SeckillMapper;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Author {那蓝桉}
 * @Date: 2023/07/28/ 9:11
 * @description
 */
@Component
public class OrderConsumer2 {
    @Autowired
    private SeckillMapper seckillMapper;
    @Autowired
    private RedisService redisService;
    @RabbitListener(queues = DelayedConfig2.DELAYED_QUEUE_NAME)
    public void receive(Message message){
        String msgCode = new String(message.getBody());
        Integer status = seckillMapper.getOrderStep(msgCode);
        if (status!=null && status == 1){
            boolean result = seckillMapper.updateOrderState(msgCode);
            System.out.println("延时队列----当前时间" + new Date() + ",修改订单"+msgCode+"订单状态为超时是否成功："+result );
            Long specificationId = seckillMapper.getSpecificationIdByOrderCode(msgCode);
            Long stock = redisService.getCacheObject("list:" + specificationId.toString());
//            将超时的订单库存加上
            stock+=1;
            redisService.setCacheObject("list:" + specificationId.toString(), stock);
        }
        //拿到code,查数据库如果没支付，确认一下，30s超时修改成超时
    }
}
